package com.ccxe.manage.service;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.ccxe.manage.dao.impl.MenuItemDao;
import com.ccxe.manage.model.MenuTree;


/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * @author shilei
 * Date: 2010-6-24 上午11:13:28
 */
public class MenuHandler extends BankBaseBusiness {

	private MenuItemDao dao = new MenuItemDao();

	/**
	 * 获取导航菜单信息列表
	 * 
	 * @param styleFlag	树类型
	 * @param userId	用户ID
	 * @return			导航树信息
	 * @throws Exception
	 */
	public List<MenuTree> getNavTreeList(int styleFlag, String userId)
			throws Exception {
		
		List<MenuTree> list = new ArrayList<MenuTree>();
		try {
			list = dao.getNavTreeList(styleFlag, userId);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}

	/**
	 * @author haofan
	 * @date 2010-6-24
	 * 功能：通过递归调用得到股票下所有子节点
	 */
	public Element getLeftMenuStr(List<MenuTree> list, Element rootElement, String parentId){
		for (MenuTree sysModuleVO : list) {
			if (parentId.equals(sysModuleVO.getParent_id()) && !parentId.equals(sysModuleVO.getMenu_id())) {
				String moduleId = sysModuleVO.getMenu_id();
				Element itemElement = rootElement.addElement("item");
				itemElement.addAttribute("text", sysModuleVO.getMenu_nm());
				if (sysModuleVO.getIs_leaf() == 1) {	//如果是叶子节点
					itemElement.addAttribute("id", moduleId);
					itemElement.addAttribute("im0", "iconText.gif");
					itemElement.addAttribute("im1", "iconText.gif");
					itemElement.addAttribute("im2", "iconText.gif");
				} else {	//如果不是叶子节点,调用方法本身
					itemElement.addAttribute("id", moduleId);
					itemElement.addAttribute("im0", "folderClosed.gif");
					itemElement.addAttribute("im1", "folderOpen.gif");
					itemElement.addAttribute("im2", "folderClosed.gif");
					getLeftMenuStr(list,itemElement,sysModuleVO.getMenu_id());
				}
			}
		}
		return rootElement;
	}
	
	/**
	 * @author haofan
	 * @date 2010-6-24
	 * 功能：加载左侧菜单树
	 */
	public String loadModuleXML(String moduleId) {
		/** 建立document对象 */
		Document document = DocumentHelper.createDocument();
		/** 建立XML文档的根tree */
		Element treeElement = document.addElement("tree");
		treeElement.addAttribute("id", "0");
		/** 建立XML文档的根节点item */
		try {
		List<MenuTree> list = dao.findMenuItemByMenuId(moduleId);

		MenuTree sysModuleVO = (MenuTree)dao.getMenuById(moduleId);
		
		if (sysModuleVO.getIs_leaf() == 1) {
			Element rootElement = treeElement.addElement("item");
			rootElement.addAttribute("id", sysModuleVO.getMenu_id());
			rootElement.addAttribute("text", sysModuleVO.getMenu_nm());
			rootElement.addAttribute("open", "1");
			rootElement.addAttribute("im0", "iconText.gif");
			rootElement.addAttribute("im1", "iconText.gif");
			rootElement.addAttribute("im2", "iconText.gif");
		} else {
//			rootElement.addAttribute("id", sysModuleVO.getId());
//			rootElement.addAttribute("text", sysModuleVO.getModuleName());
//			rootElement.addAttribute("open", "1");
//			rootElement.addAttribute("im0", "folderClosed.gif");
//			rootElement.addAttribute("im1", "folderOpen.gif");
//			rootElement.addAttribute("im2", "folderClosed.gif");
			/** 建立XML文档的节点item */
			if (null != list && 0 < list.size()) {
				getLeftMenuStr(list,treeElement, sysModuleVO.getMenu_id());
			}
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return document.asXML();
	}
	
	/**
	 * @author shilei
	 * @date 2010-6-24
	 * 功能：加载横向菜单
	 */
//	public String loadMenu(String parentId){
//		StringBuffer sb = new StringBuffer();
//		List<SysModuleVO> list = dao.findChildrenById(parentId);
//		for (SysModuleVO sysModuleVO : list) {
//			sb.append("<a class=\"menuButton\" onclick=\"loadTree('"+sysModuleVO.getId()+"');\"><span>"+sysModuleVO.getModuleName()+"</span></a>");
//		}
//		return sb.toString();
//	}
	
	/**
	 * @author	haofan
	 * @throws Exception 
	 * @date 2010-6-24
	 * 功能：加载横向菜单
	 * 第二种方案
	 * 打开一个新页面加载股票的下级菜单
	 */
	public String loadMenuSecond(String parentId,String userId) throws Exception{
		StringBuffer sb = new StringBuffer();
		//保留首页链接，其他的被股票下级覆盖
		List<MenuTree> list = dao.getMenuListByPid(0,parentId,userId);
		for (MenuTree sysModuleVO : list) {
			/**
			 * 如果是叶子节点 则加onclick事件；如果是父节点则加onmouseover事件
			 */
			if (sysModuleVO.getIs_leaf() == 1) {
				sb.append("<a id=\""+sysModuleVO.getMenu_id()+"\" onmousedown=\"onMouseDownMenu(this);\" onclick=\"onClickMenu('"+sysModuleVO.getMenu_id()+"')\"><span>"+sysModuleVO.getMenu_nm()+"</span></a>");
			} else if(sysModuleVO.getIs_leaf() == 0&& sysModuleVO.getMenu_id().equals("1005")){
				sb.append("<a id=\""+sysModuleVO.getMenu_id()+"\" onclick=\"loadTree('"+sysModuleVO.getMenu_id()+"')\"><span>"+sysModuleVO.getMenu_nm()+"</span></a>");
			}else if(sysModuleVO.getIs_leaf() == 0&& sysModuleVO.getMenu_id()!="1005"){
				sb.append("<a id=\""+sysModuleVO.getMenu_id()+"\" onmouseover=\"buttonMouseover(event, '"+sysModuleVO.getMenu_id()+"');\"><span>"+sysModuleVO.getMenu_nm()+"</span></a>");

			}
		}
		return sb.toString();
	}
	
	/**
	 * @author shilei
	 * @throws Exception 
	 * @date 2010-6-24
	 * 功能：加载下拉菜单
	 * 第二种方案
	 * 鼠标放到二级菜单节点(如：深度资料，市场统计)上以下拉形式展示第三级节点
	 */
	public String loadSecondMenu(String parentId,String userId) throws Exception{
		StringBuffer sb = new StringBuffer();
		List<MenuTree> list = dao.getMenuListByPid(0,parentId,userId);
		for (MenuTree sysModuleVO : list) {
			if (sysModuleVO.getIs_leaf() == 1) {
				sb.append("<a class=\"menuItem\" id=\""+sysModuleVO.getMenu_id()+"\" onclick=\"onClickMenu('"+sysModuleVO.getMenu_id()+"')\">"+sysModuleVO.getMenu_nm()+"</a>");
			} else {
				sb.append("<a class=\"menuItem\" id=\""+sysModuleVO.getMenu_id()+"\" onclick=\"loadTree('"+sysModuleVO.getMenu_id()+"')\">"+sysModuleVO.getMenu_nm()+"</a>");
			}
			
		}
		return sb.toString();
	}
//	
//	/**
//	 * @author haofan
//	 * @date 2010-6-24
//	 * 功能：根据主键查询MenuTree对象
//	 */
	public String findForwardURL(String id ) throws Exception {
		String url = "";
		MenuTree sysModuleVO = dao.getMenuById(id);
		if (sysModuleVO.getIs_leaf() == 1) {
			url = sysModuleVO.getMenu_url();
		}
		return url;
	}
//	
//	/**
//	 * @author haofan
//	 * @date 2010-6-24
//	 * 功能：加载当前位置导航
//	 */
	public String loadPosition(String moduleId) throws Exception{
		StringBuffer sb = new StringBuffer();
		sb.append("当前位置：");
		List<MenuTree> list = dao.findMenuItemByMenuId(moduleId);
		for (MenuTree sysModuleVO : list) {
			sb.append(sysModuleVO.getMenu_nm());
			sb.append("&gt;");
		}
		String position = sb.toString();
		position = position.substring(0, position.length()-4);
		return position;
	}
	
/**
 * 判断是否有权限
 * @param userId
 * @param url
 * @return
 * @throws Exception
 */
	public boolean canUse(String userId,String url) throws Exception{
		boolean flag = true;
		int  rs = dao.canUseMenu(userId,url);
		if(rs == 0){
			flag = false;
		}
		return flag;
	}
}